<?php
/**
 * @author GhostWei
 * @date Sep 5, 2012
 */
class front_controller_panel extends system_controller_action {

	public $lang = 'en_us';
	protected $client = 'web';
	protected $user = null;
	public function init($args = array()) {
		$this->layout()->setLayout('layout/default');
		if($this->getRequest()->getControllerName() == 'index' && $this->getRequest()->getActionName() == 'error') {
			return true;
		}
		$instance = system_passport_instance::getInstance();

		if(!($_client = $instance->getSession()->getClient())) {
			$_client = $instance->getSession()->setClient($this->getRequest()->isMobile() ? 'mobile' : 'web')->getClient();
		}
		$client = new system_setting_client($_client);
		if(1 && $tpl = $client->getTemplate()) {
			$mainTemplate = system_view_abstract::getTemplate();
			system_view_abstract::setTemplate(array('../template/' . $mainTemplate . '/' . $tpl, $mainTemplate));
		}
		$this->getResponse()->setHeader("Content-type", $client->getContentType());			

		$this->lang = system_nav_language::getInstance()->getName();
		$this->view->user = $this->user = $instance->getUser();		
		$this->view->client = $this->client = strtolower($instance->getSession()->getClient());
		return true;
	}
	
	protected function language($index, $params = array()) {
		return system_nav_language::getInstance()->get($index, $params);
	}
	
	/**
	 * 防止前台报错(non-PHPdoc)
	 * @see system_controller_action::__call()
	 */
	public function __call($method, $args) {
		if(preg_match('/^action_(.*)$/', $method, $matches)) {
			throw new system_exception('502 Not Found', 502);
		}
		return parent::__call($method, $args);
	}

	/**
	 * @return system_nav_crumbs
	 */
	protected function _crumbs() {
		return system_nav_crumbs::getInstance();
	}
		
	protected function _goRoute($name, array $urlOptions = array(), $reset = false) {
		$this->redirector()->gotoRouteAndExit($name, $urlOptions, $reset);
	}
	
	protected function _post($name = null, $default = null) {
		return $this->getRequest()->getPost($name, $default);
	}
	
	protected function _get($name = null, $default = null) {
		return $this->getRequest()->getQuery($name, $default);
	}
	
	protected function _param($name, $default = null) {
		return $this->getRequest()->getParam($name, $default);
	}
	
	protected function _redirector($url, array $options = array()) {
		if(!$url) {
			$this->_goRoute('index/index');
		}
		if(!isset($options['prependBase'])) {
			$options['prependBase'] = false;
		}
		return parent::_redirector($url, $options);
	}
	
}
